package exception;


import enums.BaseRespCodeEnum;
import enums.RespCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

/**
 * @Desciption: 合生活自定义异常
 * @author: Jacky Chai
 * @date: 2019/4/19
 */
public class AncException extends RuntimeException {

    private Integer code;
    private String message;
    private String causedBy;

    public AncException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    public AncException(RespCodeEnum codeEnum, String message) {
        this(codeEnum.getCode(),message);
    }

    public AncException(RespCodeEnum codeEnum) {
        super(codeEnum.getMsg());
        this.code = codeEnum.getCode();
        this.message = codeEnum.getMsg();
    }

    public AncException(RespCodeEnum codeEnum, Throwable cause) {
        super(codeEnum.getMsg(), cause);
        this.code = codeEnum.getCode();
        this.message = codeEnum.getMsg();
        this.causedBy = ExceptionUtils.getStackTrace(cause);
    }

    public AncException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
        this.causedBy = ExceptionUtils.getStackTrace(cause);
    }

    public AncException(String message) {
        super(message);
        this.code = BaseRespCodeEnum.PARAMS_ILLEGAL.getCode();
        this.message = message;
    }

    public AncException(String message, Throwable cause) {
        super(message, cause);
        this.code = BaseRespCodeEnum.PARAMS_ILLEGAL.getCode();
        this.message = message;
        this.causedBy = ExceptionUtils.getStackTrace(cause);
    }
    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setCausedBy(String causedBy) {
        this.causedBy = causedBy;
    }

    /**
     * 获取msg信息
     * @return
     */
    public String getSimpleMsg() {
        return this.message;
    }

    public Integer getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        String msg = "{" +
            "\"code\":" + code +
            ", \"message\":\"" + message + "\"";
        if (StringUtils.isNotEmpty(causedBy)) {
            return msg + ", \"causedBy\":\"" + causedBy.replace("\"", "") + "\"}";
        }
        return msg + "}";
    }

}
